﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using myFinance.Domain;
using myFinance.Services;
namespace myFinance.Web.Controllers {
    public class CategoryController : Controller {
        ICategoryService _categoryService = null;


        public CategoryController(ICategoryService categoryService) {
            _categoryService = categoryService;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult List(int? page) {
            var categories = _categoryService.GetCategories(page ?? 0, 10);
            return View(categories);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add() {
            return View("SaveCategory");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int id) {

            Category category = _categoryService.Read(id);
            return View("SaveCategory",category);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(FormCollection  form) {

            Category category = new Category();
            UpdateModel(category, new[] { "Name", "Description", "CategoryType" });
            //Validations
            EnsureValidations(category);
            if(ModelState.Count>0)
            {
                return View("SaveCategory");
            }
            //Update ID for Edit
            int id;
            if(int.TryParse(form["CID"],out id)){
                category.ID = id;
            }
            _categoryService.Save(category);
            return RedirectToAction("List");
        }    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(int id) { 
            _categoryService.Delete(id);
            return RedirectToAction("List");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="category"></param>
        private void EnsureValidations(Category category) {
            if (string.IsNullOrEmpty(category.Name)) {
                ModelState.AddModelError("Name", "Name should be specified");
            }
            if (string.IsNullOrEmpty(category.Name)) {
                ModelState.AddModelError("Description", "Description should be specified");
            }
            if (string.IsNullOrEmpty(category.Name)) {
                ModelState.AddModelError("CategoryType", "CategoryType should be specified");
            }
        }
    }
}
